/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FramePrincipal.java
 *
 * Created on 06/07/2011, 01:15:45 AM
 */
package com.igorov.efficientsoft.application.desktopapplication;

import com.igorov.efficientsoft.application.iniciales.ManInicial;
//import com.igorov.efficientsoft.application.pagos.ManPago;
import com.igorov.efficientsoft.application.pedidos.ManPedido;
import com.igorov.efficientsoft.application.usuarios.ManUsuarios;
import com.igorov.efficientsoft.application.util.InterfaceFuncion;
import com.igorov.efficientsoft.business.gestorpago.GestorPago;
import com.igorov.efficientsoft.business.gestorpedido.GestorPedido;
import com.igorov.efficientsoft.business.gestorusuario.GestorUsuario;
import com.igorov.efficientsoft.services.TareaDTO;
import com.igorov.efficientsoft.services.sesion.Sesion;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.JPanel;
import org.springframework.context.ApplicationContext;

/**
 *
 * @author igorov
 */
public class FramePrincipal extends javax.swing.JFrame {

    /** Creates new form FramePrincipal */
    public FramePrincipal() {
        initComponents();
    }
    
    public FramePrincipal(ApplicationContext ctx, List<TareaDTO> tareaDTOs, GestorUsuario gestorUsuario,
                            EfficientSoftView obj) {
        initComponents();
        this.obj = obj;
        this.setTitle("EfficientSoft");
        this.ctx = ctx;
        this.gestorUsuario = gestorUsuario;
        iniciarGestores();
        
        this.tareaDTOs = tareaDTOs;
        mapa = new HashMap<String, InterfaceFuncion>();
        llenarMapaFunciones();
        iniciarPaneles();
    }
    
    private void iniciarGestores(){
        gestorPedido = (GestorPedido) ctx.getBean("gestorPedido");
        gestorPago = (GestorPago) ctx.getBean("gestorPago");
    }
    
    public void iniciarPaneles(){
        int ancho = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
        int alto = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
        
        for (Iterator<TareaDTO> it = tareaDTOs.iterator(); it.hasNext();) {
            TareaDTO tareaDTO = it.next();
            llamadaFuncion(tareaDTO.getDescripcion()).funcion(ancho, alto);
        }
        
    }
    
    private void llenarMapaFunciones(){
        mapa.put("Gestionar usuarios", new InterfaceFuncion() {
            public void funcion(int ancho, int alto) {
                colocarPanelUsuarios(ancho, alto);
            }
        });
        
        mapa.put("Gestionar perfiles", new InterfaceFuncion() {
            public void funcion(int ancho, int alto) {
                colocarPanelPerfiles(ancho, alto);
            }
        });
        
        mapa.put("Reportes", new InterfaceFuncion() {
            public void funcion(int ancho, int alto) {
                colocarPanelReportes(ancho, alto);
            }
        });
        
        mapa.put("Entregar producto", new InterfaceFuncion() {
            public void funcion(int ancho, int alto) {
                colocarPanelEntregaProductos(ancho, alto);
            }
        });
        
        mapa.put("Generar inicial", new InterfaceFuncion() {
            public void funcion(int ancho, int alto) {
                colocarPanelInicial(ancho, alto);
            }
        });
        
        mapa.put("Gestionar cobros", new InterfaceFuncion() {
            public void funcion(int ancho, int alto) {
                colocarPanelCobros(ancho, alto);
            }
        });
        
        mapa.put("Gestionar pedidos", new InterfaceFuncion() {
            public void funcion(int ancho, int alto) {
                colocarPanelPedidos(ancho, alto);
            }
        });
    }
    
    public void colocarPanelUsuarios(int ancho, int alto){
        panelUsuarios = new JPanel();
        javax.swing.GroupLayout jPanelUsuarioLayout = new javax.swing.GroupLayout(panelUsuarios);
        panelUsuarios.setLayout(jPanelUsuarioLayout);
        jTabbedPane1.addTab("Gestion de usuarios", panelUsuarios);
        
        manUsuarios = new ManUsuarios(gestorUsuario, ctx);
        manUsuarios.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.panelUsuarios.add(manUsuarios.getPanel());
    }
    
    public void colocarPanelPedidos(int ancho, int alto){
        panelPedidos = new JPanel();
        javax.swing.GroupLayout jPanelPedidoLayout = new javax.swing.GroupLayout(panelPedidos);
        panelPedidos.setLayout(jPanelPedidoLayout);
        jTabbedPane1.addTab("Gestion de Pedidos", panelPedidos);
        
        manPedidos = new ManPedido(gestorPedido, ctx);
        manPedidos.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.panelPedidos.add(manPedidos.getPanel());
    }
    
    public void colocarPanelCobros(int ancho, int alto){
        panelCobros = new JPanel();
        javax.swing.GroupLayout jPanelPedidoLayout = new javax.swing.GroupLayout(panelCobros);
        panelCobros.setLayout(jPanelPedidoLayout);
        jTabbedPane1.addTab("Gestion de cobros", panelCobros);
        
 //       manCobro = new ManPago(gestorPago, ctx);
 //       manCobro.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
 //       this.panelCobros.add(manCobro.getPanel());
    }
    
    public void colocarPanelInicial(int ancho, int alto){
        panelGeneracionInicial = new JPanel();
        javax.swing.GroupLayout jPanelPedidoLayout = new javax.swing.GroupLayout(panelGeneracionInicial);
        panelGeneracionInicial.setLayout(jPanelPedidoLayout);
        jTabbedPane1.addTab("Gestion de iniciales", panelGeneracionInicial);
        
        manInicial = new ManInicial();
        manInicial.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.panelGeneracionInicial.add(manInicial.getPanel());
    }
    
    public void colocarPanelReportes(int ancho, int alto){
        panelReportes = new JPanel();
        javax.swing.GroupLayout jPanelPedidoLayout = new javax.swing.GroupLayout(panelReportes);
        panelReportes.setLayout(jPanelPedidoLayout);
        jTabbedPane1.addTab("Reportes", panelReportes);
    }
    
    public void colocarPanelPerfiles(int ancho, int alto){
        panelPerfiles = new JPanel();
        javax.swing.GroupLayout jPanelPedidoLayout = new javax.swing.GroupLayout(panelPerfiles);
        panelPerfiles.setLayout(jPanelPedidoLayout);
        jTabbedPane1.addTab("Gestion de perfiles", panelPerfiles);
    }
    
    public void colocarPanelEntregaProductos(int alto, int ancho){
        panelEntregaProducto = new JPanel();
        javax.swing.GroupLayout jPanelPedidoLayout = new javax.swing.GroupLayout(panelEntregaProducto);
        panelEntregaProducto.setLayout(jPanelPedidoLayout);
        jTabbedPane1.addTab("Entrega de productos", panelEntregaProducto);
    }
    
    public InterfaceFuncion llamadaFuncion(String key){
        return mapa.get(key);
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 891, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                .addContainerGap())
        );

        jMenuBar1.setName("Menu"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.igorov.efficientsoft.application.desktopapplication.EfficientSoftApp.class).getContext().getResourceMap(FramePrincipal.class);
        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        Sesion sesion = Sesion.getInstancia();
        sesion.destruirSesion();
        this.setVisible(false);
        obj.limpiarCampos();
        obj.getFrame().setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FramePrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    private EfficientSoftView obj;
    private ApplicationContext ctx;
    List<TareaDTO> tareaDTOs;
    Map<String, InterfaceFuncion> mapa;
    //Paneles
    private JPanel panelUsuarios;
    private JPanel panelPedidos;
    private JPanel panelPerfiles;
    private JPanel panelReportes;
    private JPanel panelEntregaProducto;
    private JPanel panelGeneracionInicial;
    private JPanel panelCobros;
    
    private ManUsuarios manUsuarios;
    private ManPedido manPedidos;
//    private ManPago manCobro;
    private ManInicial manInicial;
//    private JPanel panelCobros;
//    private JPanel panelPerfiles;
//    private JPanel panelPedidos;
    
    private GestorUsuario gestorUsuario;
    private GestorPedido gestorPedido;
    private GestorPago gestorPago;
}
